<?php

namespace app\api\controller\shop;


use app\admin\model\Shopcharge;
use app\admin\model\Shopcoupon;
use app\admin\model\Shoporder;
use app\admin\model\Shoporderprofit;
use app\admin\model\Shoproom;
use app\admin\model\Shopstaff;
use app\admin\model\Useragent;
use app\admin\model\Usercard;
use app\admin\model\Usercoupon;
use app\admin\model\Userwithdraw;
use app\api\service\BalanceService;
use app\api\service\BankService;
use app\api\service\CommonService;
use app\common\controller\Api;

/**
 *
 */
class Coupon extends Api
{
    protected $noNeedLogin = [];
    protected $noNeedRight = ['*'];


    public function add(){

        $type = $this->request->param('type');
        $shop_id = $this->request->param('shop_id');
        $name = $this->request->param('name');
        $startTime = $this->request->param('startTime');
        $endTime = $this->request->param('endTime');
        $max_count = $this->request->param('max_count');
        $valid_day = $this->request->param('valid_day');
        $price = $this->request->param('price');
        $order_price = $this->request->param('order_price');
        $room = $this->request->param('room');
        $week = $this->request->param('week');
        $notice = $this->request->param('notice');

        $room = json_decode(htmlspecialchars_decode($room), true);
        $week = json_decode(htmlspecialchars_decode($week), true);
        $notice = json_decode(htmlspecialchars_decode($notice), true);


        $shopCouponModel = new Shopcoupon();
        $coupon = $shopCouponModel->where('shop_id',$shop_id)->where('type',$type)->where('status','doing')->find();
        if($coupon){
            if($type == 'user'){
                $this->error("您已添加了新人券，只需要添加一张即可");

            }
        }
        $rooms = array();
        foreach ($room as $key => $val) {

            $rooms[] = $val['id'];
        }

        $newWeek = array();
        foreach ($week as $key => $val) {

            if ($val['name'] == "一") {
                $newWeek[] = 1;
            }
            if ($val['name'] == "二") {
                $newWeek[] = 2;
            }
            if ($val['name'] == "三") {
                $newWeek[] = 3;
            }
            if ($val['name'] == "四") {
                $newWeek[] = 4;
            }
            if ($val['name'] == "五") {
                $newWeek[] = 5;
            }
            if ($val['name'] == "六") {
                $newWeek[] = 6;
            }
            if ($val['name'] == "日") {
                $newWeek[] = 7;
            }
        }




        $rule = array();
        $rule['room'] = $rooms;
        $rule['week'] = $newWeek;
        $rule['notice'] = $notice;
        $rule['time'] = $startTime."~".$endTime;

        if($max_count== "") $max_count = 1;

        $db = array();
        $db['name'] = $name;
        $db['shop_id'] = $shop_id;
        $db['type'] = $type;
        $db['valid_day'] = $valid_day;
        $db['amount'] = $price;
        $db['order_amount'] = $order_price;
        $db['create_time'] = time();
        $db['max_count'] = $max_count;
        $db['rule'] = json_encode($rule);

        $shopCouponModel = new Shopcoupon();
        $shopCouponModel->insert($db);
        $this->success('');
    }

    public function getList(){

        $shop_id = $this->request->param('shop_id');
        $shopCouponModel = new Shopcoupon();
        $list = $shopCouponModel->where('shop_id',$shop_id)->where('status','doing')->select();

        $shopRoomModel = new Shoproom();
        $shopModel = new \app\admin\model\Shop();
        foreach ($list as $key=>$val){
            $type = "";
            if($val['type'] == 'user'){
                $type= "新人券";
            }else if($val['type'] == 'order'){
                $type= "订单券";
            }else{
                $type= "普通券";
            }
            $list[$key]['name'] = $val['name']."-".$type;
            $list[$key]['check'] = false;
            $tag= json_decode($val['rule'],true)['notice'];
            $tags = array();
            $o = array();
            $o['name'] = "使用时间：".json_decode($val['rule'],true)['time'];
            $tag[] = $o;

            $week = json_decode($val['rule'],true)['week'];
            $w = "";
            foreach ($week as $k=>$v){
                $w .= "周".$v;
                if ($k != count($week) - 1) {
                    $w .= ",";
                }
            }
            $o = array();
            $o['name'] = "使用日期:".$w;
            $tag[] = $o;

            foreach ($tag as $k => $v) {
                $obj = array();
                $obj['id'] = ($k + 1);
                $obj['value'] = $v['name'];
                $tags[] = $obj;
            }
            $list[$key]['rule_txt'] = $tags;

            $room = json_decode($val['rule'],true)['room'];

            $rooms = array();
            foreach ($room as $k=>$v){
               $obj = array();
               $r =  $shopRoomModel->where('id',$v)->find();
               $s = $shopModel->where('id',$r['shop_id'])->find();
               $obj['shop_name'] = $s['name'];
               $obj['room_name'] = $r['name'];
               $rooms[] = $obj;
            }

            $list[$key]['rooms'] = $rooms;
        }
        $this->success('',$list);
    }

    public function disableCoupon(){
        $id = $this->request->param('id');
        $shopCouponModel = new Shopcoupon();
        $shopCouponModel->where('id',$id)->update(['status'=>'stop']);

        $this->success('成功');
    }



    public function send(){

        $id = $this->request->param('id');
        $mobile = $this->request->param('mobile');
        $shopCouponModel = new Shopcoupon();
        $coupon = $shopCouponModel->where('id',$id)->find();
        $shopModel = new \app\admin\model\Shop();
        $shop = $shopModel->where('id',$coupon['shop_id'])->find();

        if($mobile == ''){
            $this->error("手机号不能为空");
        }
        $userModel = new \app\admin\model\User();
        $user = $userModel->where('org_id',$shop['org_id'])->where('mobile',$mobile)->find();
        if(!$user){
           $this->error("用户不存在");
        }

        $endTime = strtotime("+".$coupon['valid_day']." day",time());

        $userCouponModel = new Usercoupon();
        $userCouponModel->insert(['user_id'=>$user['id'],'coupon_id'=>$coupon['id'],'create_time'=>time(),'status'=>'unuse','valid_time'=>$endTime]);

        $this->success('');
    }


}
